﻿$(document).ready(function () {
    $("input.date").datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        showOn: 'button',
        duration: '',
        onSelect: function (dateText, inst) {
            //dateText = selected date in text
            //inst - instance of datepicker
            //this - input field
            //alert(dateText);
        }
    });

    $("input.datetime").datetimepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        showOn: 'button',
        duration: '',
        stepMinute: 15,
        onSelect: function (dateText, inst) {
            //dateText = selected date in text
            //inst - instance of datepicker
            //this - input field
            //alert(dateText);
        }
    });
    
    $("fieldset.search-form legend").bind("click", function () {
        $(this).siblings().toggle();
        if ($(this).html() == "Click to Modify Search") {
            $(this).html("Search");
        } else {
            $(this).html("Click to Modify Search");
        }
        return false;
    });

    $("fieldset.collapsed legend").each(function () {
        $(this).siblings().hide();
        $(this).html("Click to Modify Search");
    });

    $("td.enquiry a").bind("click", function (e) {
        if ($(this).html() == "Confidential") {
            var href = $(this).attr("href");
            var enquiryId = href.replace("View.aspx?id=", "");

            $.get("../AjaxService.ashx?q=access&id=" + enquiryId, function (data) {
                if (data == "no") {
                    alert("You do not have access to this enquiry!");                    
                } else {
                    window.location = href;
                }
            });
            e.preventDefault();
        }
    });

    $("div.tab-container").each(function () {
        var tabDisplayed = false;
        var tabMenu = $(this).first("ul");
        var tabs = $(this).find("div");
        tabs.hide();
        
        tabMenu.find("li a").each(function (index) {            
            var linksTab = tabs.eq(index);

            if ($(this).hasClass("active")) {
                linksTab.show();
                tabDisplayed = true;
            }
            
            $(this).bind("click", function (e) {
                tabs.hide();
                tabMenu.find("li a").removeClass("active");
                
                $(this).addClass("active");
                
                if (index <= tabs.length - 1) {                    
                    linksTab.show();
                } else {
                    tabs.show();
                }

                $(this).blur();
                
                e.preventDefault();
            });
        });

        if (!tabDisplayed) {
            tabs.eq(0).show();
        }
    });
});